package cn.stylefeng.guns.modular.business.app.service.impl;

import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.app.controller.dto.ProvincialAndUrbanCodeDTO;
import cn.stylefeng.guns.modular.business.app.controller.vo.GetAreaCodeVO;
import cn.stylefeng.guns.modular.business.app.controller.vo.GetCityCodeVO;
import cn.stylefeng.guns.modular.business.app.service.ProvincialAndUrbanCodeService;
import cn.stylefeng.guns.sys.modular.user.constant.ApiPayConstant;
import cn.stylefeng.guns.sys.modular.user.entity.ProvincialAndUrbanCode;
import cn.stylefeng.guns.sys.modular.user.mapper.ProvincialAndUrbanCodeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ProvincialAndUrbanCodeServiceImpl extends ServiceImpl<ProvincialAndUrbanCodeMapper, ProvincialAndUrbanCode> implements ProvincialAndUrbanCodeService {


    /**
     * 企业用户基本信息开户获取省编码
     */
    @Override
    public ResponseData getProvinceCode() {
        //查询类型为1即省的编码信息
        List<ProvincialAndUrbanCode> urbanCodeList = this.baseMapper.selectList(new LambdaQueryWrapper<ProvincialAndUrbanCode>().eq(ProvincialAndUrbanCode::getType, 1));
        //将结果转换为响应格式返回
        return ResponseData.success(convert(urbanCodeList));
    }

    /**
     * 企业用户基本信息开户获取市编码
     */
    @Override
    public ResponseData<ProvincialAndUrbanCodeDTO> getCityCode(GetCityCodeVO getCityCodeVO) {
        //参数校验
        if (Objects.isNull(getCityCodeVO)) {
            return ResponseData.error(ApiPayConstant.ERROR_PARAM);
        }
        //根据省id和类型2查询
        List<ProvincialAndUrbanCode> urbanCodeList = this.baseMapper.selectList(new LambdaQueryWrapper<ProvincialAndUrbanCode>().eq(ProvincialAndUrbanCode::getType, 2).eq(ProvincialAndUrbanCode::getParentId, getCityCodeVO.getProvinceId()));
        return ResponseData.success(convert(urbanCodeList));
    }

    /**
     * 企业用户基本信息开户获取区编码
     */
    @Override
    public ResponseData<ProvincialAndUrbanCodeDTO> getAreaCode(GetAreaCodeVO getAreaCodeVO) {
        //参数校验
        if (Objects.isNull(getAreaCodeVO)) {
            return ResponseData.error(ApiPayConstant.ERROR_PARAM);
        }
        //根据市id和类型3查询
        List<ProvincialAndUrbanCode> urbanCodeList = this.baseMapper.selectList(new LambdaQueryWrapper<ProvincialAndUrbanCode>().eq(ProvincialAndUrbanCode::getType, 3).eq(ProvincialAndUrbanCode::getParentId, getAreaCodeVO.getCityId()));
        return ResponseData.success(convert(urbanCodeList));
    }

    /**
     * 将ProvincialAndUrbanCode集合转换为ProvincialAndUrbanCodeDTO集合
     */
    private List<ProvincialAndUrbanCodeDTO> convert(List<ProvincialAndUrbanCode> urbanCodeList) {
        return urbanCodeList.stream().map(s -> {
            ProvincialAndUrbanCodeDTO dto = new ProvincialAndUrbanCodeDTO();
            BeanUtils.copyProperties(s, dto);
            return dto;
        }).collect(Collectors.toList());
    }
}
